<template>
  <div class="mobile-ledger-list common-mobile-view">
    <div class="total-ledger">
      <span class="title">账本总金额</span>
      <span class="value">{{ formatAmount(ledgerStore.ledgerAmount) }}</span>
    </div>

    <ledger-query />

    <el-empty v-if="ledgerStore.ledgerRecordList.length === 0" />

    <div v-else class="ledger-list">
      <ledger-card v-for="(item, index) in ledgerStore.ledgerList"
                  :key="index" :item="item"/>
    </div>

    <draggable-button @click="addClick"/>
  </div>
</template>

<script setup lang="ts">
import LedgerQuery from '@/components/Ledger/LedgerQuery.vue'
import LedgerCard from '@/components/Ledger/LedgerCard.vue'
import DraggableButton from '@/components/Common/DraggableButton.vue'
import { useLedgerStore } from '@/store/ledger.ts'
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { formatAmount } from 'vue3-common/utils/numberUtil'

const ledgerStore = useLedgerStore()
const router = useRouter()

onMounted(async () => {
  // await ledgerStore.queryLedgerList()
})

const addClick = () => {
  ledgerStore.ledgerApiType = 'ADD'
  ledgerStore.currentLedgerRecord = ledgerStore.getEmptyLedgerRecord()
  ledgerStore.currentLedgerRecord.book = ledgerStore.queryInfo.book
  router.push({ name: 'LedgerDetailId', params: { id: 0 } })
}
</script>

<style lang="scss">
.mobile-ledger-list {
  display: flex;
  flex-direction: column;
  gap: 10px;

  .total-ledger {
    align-self: center;
    display: flex;
    align-items: center;
    gap: 10px;

    .title {
      font-size: 1.5rem;
    }

    .value {
      font-weight: bold;
      font-size: 1.8rem;
      color: var(--el-color-primary);
    }
  }

  .ledger-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }
}
</style>
